package com.example.studentmanagement.controller;

import com.example.studentmanagement.entity.Student;
import com.example.studentmanagement.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@RestController
@RequestMapping("/api/students")
@CrossOrigin(origins = "http://localhost:5173") // Vue开发服务器的默认端口
public class StudentController {
    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;

    @GetMapping
    public ResponseEntity<?> getAllStudents() {
        try {
            List<Student> students = studentService.getAllStudents();
            return ResponseEntity.ok(students);
        } catch (Exception e) {
            logger.error("获取学生列表失败", e);
            return ResponseEntity.badRequest().body("获取学生列表失败：" + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getStudentById(@PathVariable Long id) {
        try {
            Student student = studentService.getStudentById(id);
            return ResponseEntity.ok(student);
        } catch (Exception e) {
            logger.error("获取学生信息失败", e);
            return ResponseEntity.badRequest().body("获取学生信息失败：" + e.getMessage());
        }
    }

    @PostMapping
    public ResponseEntity<?> createStudent(@RequestBody Student student) {
        try {
            // 处理性别转换
            if (student.getGender() != null) {
                student.setGender(Student.Gender.fromDisplayName(student.getGender().getDisplayName()));
            }
            Student savedStudent = studentService.createStudent(student);
            return ResponseEntity.ok(savedStudent);
        } catch (Exception e) {
            logger.error("创建学生失败", e);
            return ResponseEntity.badRequest().body("创建学生失败：" + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> updateStudent(@PathVariable Long id, @RequestBody Student student) {
        try {
            // 处理性别转换
            if (student.getGender() != null) {
                student.setGender(Student.Gender.fromDisplayName(student.getGender().getDisplayName()));
            }
            Student updatedStudent = studentService.updateStudent(id, student);
            return ResponseEntity.ok(updatedStudent);
        } catch (Exception e) {
            logger.error("更新学生信息失败", e);
            return ResponseEntity.badRequest().body("更新学生信息失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteStudent(@PathVariable Long id) {
        try {
            studentService.deleteStudent(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            logger.error("删除学生失败", e);
            return ResponseEntity.badRequest().body("删除学生失败：" + e.getMessage());
        }
    }
} 